package com.haralabidis.jdeveloper.extension.subversionmonitor.addin;


import com.haralabidis.jdeveloper.extension.subversionmonitor.addin.preferences.SubversionMonitorPreferences;

import java.awt.GridLayout;

import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.SpinnerNumberModel;

import oracle.ide.extension.RegisteredByExtension;
import oracle.ide.panels.DefaultTraversablePanel;
import oracle.ide.panels.TraversableContext;

import oracle.javatools.ui.layout.FieldLayoutBuilder;


/**
 * SubversionMonitorPreferences preference page implementation.
 */
@RegisteredByExtension("com.haralabidis.jdeveloper.extension.subversionmonitor")
final class SubversionMonitorPreferencesPagePanel extends DefaultTraversablePanel {

    private final JTextField subversionUrl = new JTextField();
    private final JTextField subversionUser = new JTextField();
    private final JPasswordField subversionPassword = new JPasswordField();
    private final JSpinner subversionMonitorTimeout =
        new JSpinner(new SpinnerNumberModel(10, 10, 3600, 10));
    private final JCheckBox subversionMonitorDisabled = new JCheckBox();
    private final JSpinner maxRevisionsToView =
        new JSpinner(new SpinnerNumberModel(500, -1, Integer.MAX_VALUE, 1));

    public SubversionMonitorPreferencesPagePanel() {
        layoutControls();
    }

    private void layoutControls() {
        FieldLayoutBuilder b = new FieldLayoutBuilder(this);

        // a logo image
        JPanel panelLogo = new JPanel(new GridLayout(1, 1));
        JLabel picture = new JLabel();
        ImageIcon image =
            new ImageIcon(SubversionMonitorPreferencesPagePanel.class.getResource("preferences/logo.jpg"));
        picture.setIcon(image);
        panelLogo.add(picture);
        b.addVerticalField("", panelLogo);

        b.add(b.field().label().withText("&Repository URL:").component(subversionUrl));
        b.add(b.field().label().withText("&Username:").component(subversionUser));
        b.add(b.field().label().withText("&Password:").component(subversionPassword));
        //((JSpinner.DefaultEditor)subversionMonitorTimeout.getEditor()).getTextField().setEditable(false);
        b.add(b.field().label().withText("&Monitor Repository Every (seconds):").component(subversionMonitorTimeout));
        b.add(b.field().label().withText("Ma&ximum &Revisions to View in Editor:").component(maxRevisionsToView));
        b.add(b.field().label().withText("&Disable Monitoring:").component(subversionMonitorDisabled));

        b.addVerticalSpring();
    }

    public void onEntry(TraversableContext context) {
        final SubversionMonitorPreferences preferences =
            getSubversionPreferences(context);
        subversionUrl.setText(preferences.getSubversionUrl());
        subversionUser.setText(preferences.getSubversionUser());
        subversionPassword.setText(preferences.getSubversionPassword());
        subversionMonitorTimeout.setValue(preferences.getMonitorTimeout());
        subversionMonitorDisabled.setSelected(preferences.getMonitorDisabled());
        maxRevisionsToView.setValue(preferences.getMaxRevisionsToView());
    }

    public void onExit(TraversableContext context) {
        final SubversionMonitorPreferences preferences =
            getSubversionPreferences(context);
        preferences.setSubversionUrl(subversionUrl.getText().trim());
        preferences.setSubversionUser(subversionUser.getText().trim());
        preferences.setSubversionPassword(subversionPassword.getText());
        preferences.setMonitorTimeout(Integer.parseInt(subversionMonitorTimeout.getValue().toString()));
        preferences.setMonitorDisabled(subversionMonitorDisabled.isSelected());
        preferences.setMaxRevisionsToView(Integer.parseInt(maxRevisionsToView.getValue().toString()));

        SubversionMonitorAddin.monitor.init(subversionUrl.getText().trim(),
                                            subversionUser.getText().trim(),
                                            subversionPassword.getText(),
                                            !subversionMonitorDisabled.isSelected(),
                                            Integer.parseInt(subversionMonitorTimeout.getValue().toString()));
    }

    private static SubversionMonitorPreferences getSubversionPreferences(TraversableContext tc) {
        return SubversionMonitorPreferences.getInstance(tc.getPropertyStorage());
    }
}
